﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomErp.Model;
using RkLibs.Functions;
namespace CustomErp.Forms
{
    public partial class FormFields : Form
    {
        ListViewItem lvi;
        public FormFields()
        {
            InitializeComponent();
        }

        private void FormFields_Load(object sender, EventArgs e)
        {
            string[] Arr = Enum.GetNames(typeof(FieldType));
            this.cbbDataType.DataSource = Arr;
            this.cbbDataType.SelectedIndex = 0;
            CommonFunction.SetDoubleBuffered(this.listViewFields);
            //EditViewColumnHeader ch = new EditViewColumnHeader(ListEditViewColumnStyle.Control);
        }

        private void listViewFields_MouseUp(object sender, MouseEventArgs e)
        {
            lvi = this.listViewFields.GetItemAt(e.X, e.Y);
            if (lvi != null)
            {
                //获取选中行的Bounds 
                Rectangle Rect = lvi.Bounds;
                int LX = listViewFields.Columns[0].Width;
                int RX = listViewFields.Columns[0].Width + listViewFields.Columns[1].Width;

                if (!(e.X > LX && e.X < RX && e.Y < Rect.Bottom && e.Y > Rect.Top))
                {
                    this.cbbDataType.Visible = false;
                    return;
                }
                this.cbbDataType.Visible = false;
                Rect.X = listViewFields.Left + listViewFields.Columns[0].Width + 2;
                Rect.Y = this.listViewFields.Top + 2 + Rect.Y;
                Rect.Width = listViewFields.Columns[1].Width + 2;
                this.cbbDataType.Bounds = Rect;
                this.cbbDataType.Text = lvi.SubItems[1].Text;
                this.cbbDataType.Visible = true;
                this.cbbDataType.BringToFront();
                this.cbbDataType.Focus();
            }
            else
            {
                this.cbbDataType.Visible = false;
            }

        }

        private void cbbDataType_SelectedIndexChanged(object sender, EventArgs e)
        {
            lvi.SubItems[1].Text = this.cbbDataType.Text;
        }

        private void cbbDataType_MouseLeave(object sender, EventArgs e)
        {
            //lvi.SubItems[1].Text = cbbDataType.Text;
        }

        private void ToolStripaddNew_Click(object sender, EventArgs e)
        {
            ListViewItem item = new ListViewItem();
            item.Text = "新增";
            item.SubItems.Add(FieldType.Text.ToString());
            listViewFields.Items.Add(item);
        }

        private void listViewFields_MouseDown(object sender, MouseEventArgs e)
        {
            this.cbbDataType.Visible = false;
        }

    }
}
